package org.rcloud.io.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.rcloud.io.Utils.PageVo;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.model.Atlas;
import org.rcloud.io.service.AtlasService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by Administrator on 2019/2/28.
 */
@RestController
@RequestMapping(value = "/api")
public class AtlasController {

    @Autowired
    private AtlasService atlasService;


    /*
    *   保存地理地址信息
    * */
    @ApiOperation(value="保存地理地址信息", notes="保存地理地址信息")
    @ResponseBody
    @RequestMapping(value = "/saveAtlas", produces = {"application/json;charset=UTF-8"})
    public ResultVo saveAtlas(@RequestBody String params){

        JSONObject object = JSON.parseObject(params);

        Map map = JSON.parseObject(object.toString());
        String num = map.get("id").toString();

        String name = map.get("name").toString();
        String address = map.get("address").toString();
        String addressType = map.get("addressType").toString();
        String longitude = map.get("longitude").toString();
        String latitude = map.get("latitude").toString();
        String etype = map.get("etype").toString();
        String summary = map.get("summary").toString();
        String distinctive = map.get("distinctive").toString();
        String unit = map.get("unit").toString();
        String report = map.get("report").toString();
        String username = map.get("username").toString();
        String phonetype = map.get("phonetype").toString();
        String userphone = map.get("userphone").toString();
        String townsname = map.get("townsname").toString();
        String townsphone = map.get("townsphone").toString();

        Atlas atlas = new Atlas();
        atlas.setId(UUID.randomUUID().toString());
        atlas.setNum(num);
        atlas.setTitle(name);
        atlas.setAddress(address);
        atlas.setAddresstype(addressType);
        atlas.setEtype(etype);
        atlas.setLongitude(longitude);
        atlas.setLatitude(latitude);
        atlas.setSummary(summary);
        atlas.setDistinctive(distinctive);
        atlas.setUnit(unit);
        atlas.setReport(report);
        atlas.setUsername(username);
        atlas.setPhonetype(phonetype);
        atlas.setUserphone(userphone);
        atlas.setTownsname(townsname);
        atlas.setTownsphone(townsphone);

        return atlasService.saveAtlas(atlas);
    }

     /*
    *   查询全部地理地址信息
    * */
     @ApiOperation(value="查询全部地理地址信息", notes="查询全部地理地址信息")
     @ResponseBody
     @RequestMapping(value = "/findAllAtlas", produces = {"application/json;charset=UTF-8"})
     public PageVo<Atlas> findAllAtlas() {
         return atlasService.findAllAtlas();
     }


    /*
       * 根据id获取地理地址信息
       * */
    @ApiOperation(value="获取地理地址信息", notes="根据id获取地理地址信息")
    @ResponseBody
    @RequestMapping(value = "/findAtlasById", produces = {"application/json;charset=UTF-8"})
    public ResultVo findEndorsementById(@RequestBody String params){
        String[] ids = params.split("=");
        return atlasService.findAtlasById(ids[1]);
    }


     /*
       * 根据id修改地理地址信息
       * */
    @ApiOperation(value="修改地理地址信息", notes="根据id修改地理地址信息")
    @ResponseBody
    @RequestMapping(value = "/updateAtlasById", produces = {"application/json;charset=UTF-8"})
    public ResultVo updateAtlasById(@RequestBody String params){
        JSONObject object = JSON.parseObject(params);

        Map map = JSON.parseObject(object.toString());

        String id = map.get("id").toString();
        String num = map.get("num").toString();
        String name = map.get("name").toString();
        String address = map.get("address").toString();
        String addressType = map.get("addressType").toString();
        String etype = map.get("etype").toString();
        String longitude = map.get("longitude").toString();
        String latitude = map.get("latitude").toString();
        String summary = map.get("summary").toString();
        String distinctive = map.get("distinctive").toString();
        String username = map.get("username").toString();
        String phonetype = map.get("phonetype").toString();
        String userphone = map.get("userphone").toString();
        String townsname = map.get("townsname").toString();
        String townsphone = map.get("townsphone").toString();
        String report = map.get("report").toString();
        String unit = map.get("unit").toString();

        Atlas atlas = new Atlas();

        atlas.setId(id);
        atlas.setNum(num);
        atlas.setTitle(name);
        atlas.setAddress(address);
        atlas.setAddresstype(addressType);
        atlas.setEtype(etype);
        atlas.setLongitude(longitude);
        atlas.setLatitude(latitude);
        atlas.setSummary(summary);
        atlas.setDistinctive(distinctive);
        atlas.setUnit(unit);
        atlas.setReport(report);
        atlas.setUsername(username);
        atlas.setPhonetype(phonetype);
        atlas.setUserphone(userphone);
        atlas.setTownsname(townsname);
        atlas.setTownsphone(townsphone);

        return atlasService.updateById(atlas);
    }


    /*
      * 根据num获取地理地址信息
      * */
    @ApiOperation(value="根据num获取地理地址信息", notes="根据num获取地理地址信息")
    @ResponseBody
    @RequestMapping(value = "/findAtlasByNum", produces = {"application/json;charset=UTF-8"})
    public ResultVo findAtlasByNum(@RequestBody String params){
        JSONObject object= JSON.parseObject(params);
        return atlasService.findAtlasByNum(object.getString("num"));
    }
}
